package com.system.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.frame.model.MiniPager;
import com.frame.util.MiniuiUtil;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.system.model.DataMgrConfig;

/**
 * @author LRC
 * @createDate 2015年6月5日
 * @description 综合管理类服务层 
 */
public class ManagerService {
	public static String model = "系统管理";
	
	
	/**
	 * @updateTeim 2015年6月6日下午10:13:58
	 * @description 查询系统数据
	 * @param c
	 * @return
	 */
	public static MiniPager findSysDataList(Controller c){
		if(c==null)return null;
		//获取目标列表id
		int targetID = c.getParaToInt("targetID");
		//查询目标数据对象
		DataMgrConfig config = DataMgrConfig.dao.findById(targetID);
		if(config==null)return null;
		String searchArg  = config.getStr("searchArg");
		String querySql = config.getStr("querySql");
		String select = querySql.substring(0,querySql.indexOf(" from "));
		if(select ==null || select.indexOf("select")==-1)return null;
		String from = querySql.substring(querySql.indexOf(" from "));
		if(from.indexOf("where")==-1){
			from += " where 1=1 ";
		}
		StringBuilder sql = new StringBuilder(from);
		String[] paramNames = searchArg==null? null:searchArg.split(",");
		
		MiniPager pager = MiniPager.getMiniPager(c, paramNames, sql);
		
		if(pager.sortField==null||"".equals(pager.sortField)){
			//sql.append(" order by inserTime desc");
		}
		
		System.out.println(select);
		System.out.println(sql);
		
		pager.execQuery(select, sql.toString());
		
		return pager;
	}
	
	/**
	 * @updateTeim 2015年6月6日下午10:13:41
	 * @description 保存系统数据
	 * @param c
	 * @return
	 */
	public static boolean saveSysData(Controller c){
		//获取目标列表id
		int targetID = c.getParaToInt("targetID");
		//查询目标数据对象
		DataMgrConfig config = DataMgrConfig.dao.findById(targetID);
		if(config==null)return false;
		String tableName = config.getStr("mainTableName");
		String keyName = config.getStr("keyName");
		if(keyName!=null){
			String data = c.getPara("data");
			Map<String,List<Record>> map = MiniuiUtil.parseGridChange(data, keyName.toString());
			//String insert = "insert into version(versionNum,userID) values(?,?)";
			//String update = "update version set versionNum=? where versionID=? and verRelease is null";
			String delete = "delete from "+tableName+" where "+keyName+"=? ";
			//Db.batch(insert, "versionNum,userID", map.get("insert"), 5);
			//Db.batch(update, "versionNum,versionID", map.get("update"), 5);
			List<Record> insert = map.get("insert");
			List<Record> update = map.get("update");
			for(Record r : insert){
				Db.save(tableName, keyName,r);
			}
			for(Record r : update){
				System.out.println(r);
				Db.update(tableName, keyName,r);
			}
			Db.batch(delete, keyName, map.get("delete"), 5);
			return true;
		}
		
		return false;
	}
	
	/**
	 * @updateTeim 2015年6月6日下午10:13:58
	 * @description 查询系统数据列表配置
	 * @param c
	 * @return
	 */
	public static MiniPager findDataMgrConfigList(Controller c){
		if(c==null)return null;
		String[] paramNames = new String[]{"listName:c:like","mainTableName:c:like"};
		StringBuilder sql = new StringBuilder("from sys_base_data where 1=1");
		MiniPager pager = MiniPager.getMiniPager(c, paramNames, sql);
		if(pager.sortField==null||"".equals(pager.sortField)){
			sql.append(" order by updateTime desc");
		}
		pager.execQuery("select * ", sql.toString());
		return pager;
	}
	
	/**
	 * @updateTeim 2015年6月6日下午10:13:41
	 * @description 保存系统数据列表配置
	 * @param c
	 * @return
	 */
	public static boolean saveDataMgrConfig(Controller c){
		//获取版本id
		Object id = c.getPara("id");
		if(id==null){
			String data = c.getPara("data");
			Map<String,List<Record>> map = MiniuiUtil.parseGridChange(data, "id");
			String insert = "insert into sys_base_data(listName,mainTableName,keyName,readOnly,updateTime) values(?,?,?,SYSDATE())";
			String update = "update sys_base_data set listName=?,mainTableName=?,keyName=?,readOnly=?,updateTime=SYSDATE() where id=?";
			String delete = "delete from  sys_base_data where id=?";
			Db.batch(insert, "listName,mainTableName,keyName,readOnly", map.get("insert"), 5);
			Db.batch(update, "listName,mainTableName,keyName,readOnly,id", map.get("update"), 5);
			Db.batch(delete, "id", map.get("delete"), 5);
			return true;
		}else{
			//执行保存
			DataMgrConfig obj = new DataMgrConfig();
			obj.set("id", c.getPara("id"));
			obj.set("updateTime", new Date());
			obj.set("querySql", c.getPara("querySql"));
			obj.set("searchArg", c.getPara("searchArg"));
			obj.set("columnMapping", c.getPara("columnMapping"));
			obj.set("remark", c.getPara("remark"));
			return obj.update();
		}
	}

}
